package com.powernode.controller;

import com.github.pagehelper.PageInfo;
import com.powernode.common.Constants;
import com.powernode.common.Result;
import com.powernode.domain.Customer;
import com.powernode.domain.User;
import com.powernode.dto.CustomerDto;
import com.powernode.service.CustomerService;
import com.powernode.utils.WebMvcUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("customer")
@Api(tags = "客户的管理接口")
public class CustomerController {
    @Resource
    private CustomerService customerService;

    @GetMapping("list.do")
    @ApiOperation("分页获取客户")
    public Result list(CustomerDto customerDto) {
        User user = WebMvcUtils.getCurrentUser();
        if (user.getRole().equals(Constants.ROLE_2)) {
            customerDto.setUserId(user.getId());
        }
        PageInfo<User> pageInfo = customerService.queryAllCustomer(customerDto);
        return new Result(0, "查询成功", pageInfo.getTotal(), pageInfo.getList());
    }

    @PostMapping("add.do")
    @ApiOperation("添加客户")
    public Result add(Customer customer) {
        try {
            customer.setUserId(WebMvcUtils.getCurrentUser().getId());
            customer.setCreateTime(new Date());
            customer.setDeleted(Constants.DELETED_1);
            customerService.insertSelective(customer);
            return Result.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    @PostMapping("update.do")
    @ApiOperation("修改客户")
    public Result update(Customer customer) {
        try {
            customerService.updateByPrimaryKeySelective(customer);
            return Result.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    @PostMapping("del.do")
    @ApiOperation("根据id删除一个客户")
    @ApiImplicitParam(name = "id", value = "客户ID", required = true, paramType = "query", dataType = "Integer")
    public Result del(Integer id) {
        try {
            Customer customer = new Customer();
            customer.setId(id);
            customer.setDeleted(Constants.DELETED_2);
            customerService.updateByPrimaryKeySelective(customer);
            return Result.DEL_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.DEL_ERROR;
        }
    }

    @PostMapping("changeCustomerUser.do")
    @ApiOperation("修改客户所属的业务员")
    public Result changeCustomerUser(Integer userId, Integer[] customerIds) {
        try {
            customerService.changeCustomerUser(userId, customerIds);
            return Result.OPT_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.OPT_ERROR;
        }
    }

    @PostMapping("setSession.do")
    @ApiOperation("留言时存入客户对应业务员数据")
    public Result setSession(Customer customer){
        try {
            WebMvcUtils.getSession().setAttribute(Constants.SESSION_CUSTOMER_KEY,customer);
            return Result.OPT_SUCCESS;
        }catch (Exception e) {
            return Result.OPT_ERROR;
        }
    }
}